package leetcode.day_2021_04_12_2021_04_18;

import leetcode.Head;
import leetcode.ListNode;

/**
 *  82. 删除排序链表中的重复元素 II
 *  https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
 */
public class LeetCode0082 {
    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode root = new ListNode(-1);
        root.next = head;
        ListNode pre = root;
        ListNode cur = root.next;
        while (cur != null) {
            if (cur.next!=null && cur.val==cur.next.val) {
                while (cur.next!=null && cur.val==cur.next.val) {
                    cur = cur.next;
                }
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = pre.next;
                cur = cur.next;
            }
        }
        return root.next;
    }

    public static void main(String[] args) {
        ListNode node = Head.genListEven();
        ListNode node2 = deleteDuplicates(node);
        Head.printList(node2);
    }
}
